/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m003.ui.controllers;

import com.vaadin.data.util.BeanItemContainer;
import ep.libs.implementations.AbstractModule;
import ep.persistence.controllers.T005PatientJpaController;
import ep.persistence.controllers.T005T007JpaController;
import ep.persistence.controllers.T007IllnessJpaController;
import ep.persistence.controllers.exceptions.IllegalOrphanException;
import ep.persistence.controllers.exceptions.NonexistentEntityException;
import ep.persistence.entities.T005Patient;
import ep.persistence.entities.T005T007;
import ep.persistence.entities.T007Illness;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Spyros
 */
public class M003Controller {

    private T005Patient patient;

    public M003Controller(Object patientObject, AbstractModule module) {
        this.patient = (T005Patient) patientObject;
    }

    public BeanItemContainer<T005T007> getTableContainer() {
        BeanItemContainer<T005T007> beanItemContainer;
        if (patient != null && patient.getT005T007Collection() != null && !patient.getT005T007Collection().isEmpty()) {
            beanItemContainer = new BeanItemContainer<T005T007>(T005T007.class, patient.getT005T007Collection());
        } else {
            beanItemContainer = new BeanItemContainer<T005T007>(T005T007.class);
        }
        return beanItemContainer;
    }

    public BeanItemContainer<T007Illness> getIllnessContainer() {
        T007IllnessJpaController jpaController = new T007IllnessJpaController();
        return new BeanItemContainer<T007Illness>(T007Illness.class, jpaController.findT007IllnessEntities());
    }

    public T005T007 getNewT005t007(Object illnessObject){
        T005T007 t005t007 = new T005T007();
        t005t007.setT007Illness((T007Illness)illnessObject);
        return t005t007;
    }

    public boolean checkIfIllnessExists(Object illnessObject, Collection collection){
        for (Iterator it = collection.iterator(); it.hasNext();) {
            T005T007 t005t007 = (T005T007)it.next();
            if (t005t007.getT007Illness().equals((T007Illness)illnessObject)){
                return true;
            }
        }
        return false;
    }

    public boolean save(Collection t005t007Collection) {
        try {
            patient.setT005T007Collection(t005t007Collection);
            T005T007JpaController jpaController = new T005T007JpaController();
            for (Iterator it = t005t007Collection.iterator(); it.hasNext();) {
                T005T007 t005t007 = (T005T007) it.next();
                t005t007.setT005Patient(patient);
                if (t005t007.getId() == null){
                    jpaController.create(t005t007);
                }else{
                    jpaController.edit(t005t007);
                }
            }
            return true;
        } catch (IllegalOrphanException ex) {
            Logger.getLogger(M003Controller.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(M003Controller.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(M003Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
